package com.gjzt.business.media.service;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;

import com.eastdigit.system.model.BusinessException;
import com.eastdigit.util.HttpUtil;
import com.eastdigit.util.PropertiesUtil;

/**
 * 
 * line_stop
 * 
 * @author jun 2017-4-1
 */
@Service
public class WuhanbusAPIService {
	
	private static final String WEBAPI_WUHANBUS_COM_CORPID = PropertiesUtil.getProperty("webapi.wuhanbus.com.corpid");
	private static final String WEBAPI_WUHANBUS_COM_CORPSECRET = PropertiesUtil.getProperty("webapi.wuhanbus.com.corpsecret");
	private static final String WEBAPI_WUHANBUS_COM_ACCESStOKEN_URL = PropertiesUtil.getProperty("webapi.wuhanbus.com.accessToken.url");
	private static final String WEBAPI_WUHANBUS_COM_LINES_URL = PropertiesUtil.getProperty("webapi.wuhanbus.com.lines.url");
	private static final String WEBAPI_WUHANBUS_COM_BUSES_URL = PropertiesUtil.getProperty("webapi.wuhanbus.com.buses.url");
	
	private static final String WEBAPI_WUHANBUS_COM_USER_LOGIN = PropertiesUtil.getProperty("webapi.wuhanbus.com.user.login");
	
	/**
	 * 获取accessToken
	 * @return
	 */
	private String getAccessTokenOfWebApi() {
		Map<String, String> params = new HashMap<String, String>();
		params.put("corpid", WEBAPI_WUHANBUS_COM_CORPID);
		params.put("corpsecret", WEBAPI_WUHANBUS_COM_CORPSECRET);
		String result = HttpUtil.doGet(WEBAPI_WUHANBUS_COM_ACCESStOKEN_URL, params);
		JSONObject JSONResult = JSONObject.fromObject(result);
		if(JSONResult != null && JSONResult.containsKey("access_token")){
			return JSONResult.getString("access_token");
		}
		throw new BusinessException("获取accessToken失败");
	}
	
	/**
	 * 获取线路信息
	 * @return
	 */
	public JSONArray getLines() {
		String accessToken = getAccessTokenOfWebApi();
		Map<String, String> params = new HashMap<String, String>();
		params.put("access_token", accessToken);
		params.put("method", "getlist");
		try{
			JSONArray list = null;
			String result = HttpUtil.postWithUrlEncodedFormEntity(WEBAPI_WUHANBUS_COM_LINES_URL, params);
			JSONObject JSONResult = JSONObject.fromObject(result);
			if(JSONResult != null && JSONResult.containsKey("line_list")){
				list = JSONResult.getJSONArray("line_list");
			}
			return list;
		}catch(Exception e){
			throw new BusinessException("获取线路信息失败");
		}
	}
	
	/**
	 * 获取车辆信息
	 * @return
	 */
	public JSONArray getBuses() {
		String accessToken = getAccessTokenOfWebApi();
		Map<String, String> params = new HashMap<String, String>();
		params.put("access_token", accessToken);
		params.put("key", "getlist");
		try{
			JSONArray list = null;
			String result = HttpUtil.postWithUrlEncodedFormEntity(WEBAPI_WUHANBUS_COM_BUSES_URL, params);
			JSONObject JSONResult = JSONObject.fromObject(result);
			if(JSONResult != null && JSONResult.containsKey("veh_list")){
				list = JSONResult.getJSONArray("veh_list");
			}
			return list;
		}catch(Exception e){
			throw new BusinessException("获取车辆信息失败");
		}
	}
	
	public JSONObject login(String userName, String password){
		Map<String, String> params = new HashMap<String, String>();
		params.put("method", "login");
		params.put("userid", userName);
		params.put("password", password);
		try{
			String result = HttpUtil.postWithUrlEncodedFormEntity(WEBAPI_WUHANBUS_COM_USER_LOGIN, params);
			return JSONObject.fromObject(result);
		}catch(Exception e){
			throw new BusinessException("获取车辆信息失败");
		}
	}
	




}